package cn.iocoder.yudao.module.measure.dal.dataobject.device.message.adapter;

import cn.iocoder.yudao.module.measure.dal.dataobject.device.message.DeviceMessage;
import cn.iocoder.yudao.module.measure.dal.dataobject.warning.BusinessDeviceWarnRecord;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class DeviceMessageAdapter implements DeviceMessage {
    private Object object;

    public DeviceMessageAdapter(Object object) {
        this.object = object;
    }


    @Override
    public String getCommandStr() {
        Method method = getMethod("getCommandStr");
        log.debug("getCommandStr method:{}",method);
        if(null != method){
            try {
                log.debug("getCommandStr commandStr: invoke");
                return (String) method.invoke(object);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }else{
            return null;
        }
    }

    @Override
    public byte[] getData(Integer startBitIndex, Integer endBitIndex) {
        Method method = getMethod("getData",Integer.class,Integer.class);
        if(null != method){
            try {
                return (byte[]) method.invoke(object,startBitIndex,endBitIndex);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }else{
            Method method2 = getMethod("getData");
            if(null != method2){
                try {
                    byte[] datas = (byte[])method2.invoke(object);
                    byte[] res = new byte[endBitIndex-startBitIndex+1];
                    System.arraycopy(datas,startBitIndex,res,0,endBitIndex-startBitIndex+1);
                    return res;
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }

            }else{
                return null;
            }
        }
    }

    @Override
    public List<BusinessDeviceWarnRecord> getEvents() {
        Method method = getMethod("getEvents");
        if(null != method){
            try {
                List<String> list = (List<String>) method.invoke(object);
                List<BusinessDeviceWarnRecord> eventList = new ArrayList<>();
                if(null != list){
                    for(String s : list){
                        BusinessDeviceWarnRecord payload = new BusinessDeviceWarnRecord();
                        payload.setWarningContent(s);
                        payload.setWarningCheckType("DEVICE_UPLOAD");
                        eventList.add(payload);
                    }
                    return eventList;
                }else{
                    return null;
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }else{
            return null;
        }
    }


    private Method getMethod(String s, Object... args) {
        Method[] methods = object.getClass().getMethods();
        for (Method method : methods) {
            if (method.getName().equals(s)) {
                if(null==args){
                    return method;
                }else{
                    if(method.getParameterCount() == args.length ){
                        return method;
                    }
                }
            }
        }
        return null;
    }
}
